<?php

defined('INTERNAL') || die();

class PluginBlocktypeView extends SystemBlocktype
{

    public static function get_title()
    {
        return get_string('title', 'blocktype.view');
    }

    /**
     * Optional method. If exists, allows this class to decide the title for 
     * all blockinstances of this type
     */
    public static function get_instance_title(BlockInstance $instance)
    {
        //$title = $instance->get('title');
        $views_id = self::get_views_id($instance);

        if ($views_id && count($views_id) == 1)
        {
            $view = new View(reset($views_id));
            return $view->get('title');
        }
        else
        {
            return '';
        }
    }

    public static function get_description()
    {
        return get_string('description', 'blocktype.view');
    }

    public static function get_categories()
    {
        return array('general');
    }

    public static function get_views(BlockInstance $instance)
    {
        $result = array();
        $view_ids = self::get_views_id($instance);
        foreach ($view_ids as $view_id)
        {
            $view = new View($view_id);
            $title = $view->get('title');
            $description = $view->get('description');
            
            $result[$view_id] = (object) array('id' => $view_id, 'title' => $title, 'description' => $description);
        }

        return $result;
    }

    public static function get_views_id(BlockInstance $instance)
    {
        $result = array();
        $configdata = $instance->get('configdata');

        $view_id = isset($configdata['viewid']) ? $configdata['viewid'] : array();
        $view_ids = is_array($view_id) ? $view_id : array($view_id);
        foreach ($view_ids as $view_id)
        {
            if (can_view_view($view_id))
            {
                $result[$view_id] = $view_id;
            }
        }
        return $result;
    }

    public static function render_instance(BlockInstance $instance, $editing=false)
    {
        $views = self::get_views($instance);
        $view = reset($views);

        $result = '';
        if (!empty($views))
        {
            $smarty = smarty();
            $smarty->assign('options', $configdata);
            $smarty->assign('view', $view);
            $result = $smarty->fetch('blocktype:view:view.tpl');
        }

        return $result;
    }

    public static function has_instance_config()
    {
        return true;
    }

    public static function instance_config_form($instance, $template)
    {
        $views_id = self::get_views_id($instance);

        $elements = array();
        $elements['viewid'] = self::viewchooser_element($views_id);
        return $elements;
    }

    public static function viewchooser_element($default=null)
    {
        return array(
            'name' => 'viewid',
            'type' => 'viewchooser',
            'title' => get_string('View', 'view'),
            'defaultvalue' => $default,
            'blocktype' => 'view',
            'limit' => 10,
            'selectone' => true,
            //'template'  => 'form:viewchooser-element.tpl',
        );
    }

    public static function default_copy_type()
    {
        return 'shallow';
    }

    public static function allowed_in_view(View $view)
    {
        return $view->get('owner') != null;
    }

}

?>